package Game 
{
	import flash.events.Event;
	import flash.geom.Point;
	/**
	 * ...
	 * @author Vijay Ramani
	 */
	public class EnergyPellet extends GameEntity 
	{
		private static const VERTICES:Array = [
			{x:0, y:-0.5 },
			{x:0.25, y:0 },
			{x:0, y:0.5 },
			{x:-0.25,y:0}			
		];
		
		private static const MIN_VALUE:uint = 10;
		private static const VALUE_SIZE:Number = 0.2;
		private static const CAPTURE_SIZE:Number = 10;
		
		private var moveToTarget:GameEntity = null;
		private var moveToVec:Point = new Point();
		
		public function EnergyPellet(_value:uint = MIN_VALUE) 
		{
			EnergyValue = _value;
			Init();
			
			super(GameEntity.TYPE_PELLET);
		}
		
		private function Init():void
		{
			Render();
		}
		private function Render():void
		{
			var _size:Number = 20 + EnergyValue * VALUE_SIZE;
			
			graphics.lineStyle(1, 0xFFFFFF);
			graphics.beginFill(0x5883E7);
			graphics.moveTo(_size*VERTICES[0].x, _size*VERTICES[0].y);
			for (var i:uint = 1; i < VERTICES.length; i++) graphics.lineTo(_size*VERTICES[i].x, _size*VERTICES[i].y);
			graphics.endFill();
		}
		
		public function Capture(_g:GameEntity):void
		{
			SectorRef.Remove(this);
			moveToTarget = _g;
			addEventListener(Event.ENTER_FRAME, MoveToEntity);
		}
		private function MoveToEntity(_e:Event):void
		{
			moveToVec =  moveToTarget.Pos.subtract(Pos);
			if (moveToVec.length < CAPTURE_SIZE)
			{
				removeEventListener(Event.ENTER_FRAME, MoveToEntity);
				moveToTarget.EnergyValue += EnergyValue;
				WorldRef.RemoveGameEntity(this);
			}
			else
			{
				Pos.x += (moveToVec.x) / 3;
				Pos.y += (moveToVec.y) / 3;
			}
			
		}
	}

}